﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Toolkit;
using ESRI.ArcGIS.Client.Symbols;
using System.Collections.ObjectModel;

namespace Silverlight.Arcgis.MapTip
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

        }

        private void MyGraphicsLayer_Initialized(object sender, EventArgs args)
        {
            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query()
            {
                Geometry = new ESRI.ArcGIS.Client.Geometry.Envelope(-180, 0, 0, 90)
            };
            query.OutFields.Add("*");

            QueryTask queryTask = new QueryTask(App.MapUrl+"/3");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.ExecuteAsync(query);


            //添加一个标识
            AddAGraphic();
        }

        private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;

            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            MyMapTip.GraphicsLayer = graphicsLayer;


            if (featureSet != null && featureSet.Features.Count > 0)
            {
                foreach (Graphic feature in featureSet.Features)
                {
                    feature.Symbol = DefaultFillSymbol;
                    graphicsLayer.Graphics.Add(feature);
                }
            }
        }

        /// <summary>
        /// 在图层中添加一个东东
        /// </summary>
        [System.Windows.Browser.ScriptableMember]
        public void AddAGraphic()
        {
            //得到前的图层
            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.Graphics.Add(new Graphic()
            {
                MapTip = new TextBox() { 
                 Text ="ddddddd"
                }
                ,
                Symbol = new SimpleFillSymbol()
                {
                    BorderBrush = new SolidColorBrush(Colors.Blue) {  },
                    BorderThickness = 3,
                    Fill = new SolidColorBrush(Colors.Green) { Opacity = 0.5 }
                }
              
            });

        }

    }
}
